<?php class send extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function truncate(){
    	return $this->db->truncate('send');
    }
    
    public function join_select($cond=""){

       $this->db->select('send.sendtime as sendtime,send.company_id,category.level as level,category.codes as codes, send.mod_id as mod_id,send.category_id as category_id,send.version_id as version_id,models.name AS mod_name, category.name AS category_name,category.codes AS codes, version.name AS version_name,send.count as s_count,send.codes as s_code');   
       $this->db->from('send');
       $this->db->join('models','models.mod_id = send.mod_id','left');
       $this->db->join('category','category.category_id = send.category_id','left');
       $this->db->join('version','version.version_id = send.version_id','left');
       if($cond!=""){
          $this->db->where($cond);
       }
       $this->db->order_by('category.codes','asc');
       $query = $this->db->get();
       $result = $query->result();
//echo $this->db->last_query();exit;
       return $result; 
    }
    
    function insert_code($data){
       $result = $this->db->insert('codes',$data);
       return $this->db->insert_id();
    }
    
    function select_codes(){
       $query = $this->db->get('codes');
       
       $result = $query->result(); 
       return $result;
    }
    function insert_for_excel($array){
       $this->version_id = $array['version_id'];
       $this->category_id = $array['category_id'];
       $this->mod_id = $array['mod_id'];      
       $this->codes = $array['codes']; 
       $this->sendtime = $array['sendtime'];   
       $this->company_id = $array['company_id']; 
      //$this->updatetime = $array['updatetime'];
       $this->createtime = time();
       $re = $this->select_by_array(array('sendtime'=>$array['sendtime'],'codes'=>$array['codes'],'mod_id'=>$array['mod_id'],'category_id'=>$array['category_id'],'version_id'=>$array['version_id'],'company_id'=>$array['company_id']));
       if(count($re)==0){
       	 $this->count = $array['count'];
         $this->db->insert('send',$this);
         $result = $this->db->insert_id();
       }else{
       	 $count = $re[0]->count+$array['count'];
         $this->update($re[0]->send_id,array('count'=>$count));
         $result = $re[0]->send_id;
       }
       return $result;
    }
    
    function select_where($category_id,$version_id){
       $this->db->select_sum('count');
       $this->db->where('category_id',$category_id);
       $this->db->where('version_id !=',$version_id);
       $query = $this->db->get('send');
       
       $result = $query->result(); 

       return $result[0]->count;
    }
    
    function select_by_version($update,$arr){
      if($update==0){
      	$this->db->where_in('version_id', $arr);
      }else{
      	$this->db->where_not_in('version_id', $arr);
      }
       $query = $this->db->get('send');       
       $result = $query->result(); 
       return $result;
    }
    
    function getlast_fo($version_id,$id){

       $this->db->where('category_id',$id);
       $this->db->where('version_id !=',$version_id);
       $this->db->select_sum('count');
       $query = $this->db->get('send');
       $result = $query->result(); 
       return $result;
    }
    
    function select_join($ar){
       $this->db->select('*');
       $this->db->where($ar);
	   $this->db->from('category');
	   $this->db->join('send', 'send.category_id = category.category_id');
	   $this->db->order_by('company_id','asc');
       $query = $this->db->get();
       $result = $query->result(); 
       return $result;
    
    }
    function select2($id,$type='',$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where($id);
       }
       if($type!=''){
         	$this->db->where(array('type'=>''));
       }
       $this->db->order_by('send_id','desc');
       $query = $this->db->get('send');
       
       $result = $query->result(); 
       return $result;
    }
    
    function insert($data){
       $result = $this->db->insert('send',$data);
       return $this->db->insert_id();
    }
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where(array('send_id'=>$id));
       }
       $this->db->order_by('send_id desc');
       $query = $this->db->get('send');
       
       $result = $query->result(); 
       return $result;
    }
    
    function sel($array){
       $this->db->where($array);
       $this->db->select_sum('count');
       $query = $this->db->get('send');       
       $result = $query->result(); 
       echo $this->db->last_query();
       return $result;
    }
    
    function sel2($array){
       $this->db->where($array);
       $query = $this->db->get('send');    
       $this->db->order_by('send_id asc');
       $result = $query->result(); 
       
       return $result;
    }
    
    
    function select_by_array($array){
       $this->db->where($array);
       $this->db->order_by('send_id','desc');
       $query = $this->db->get('send');       
       $result = $query->result(); 
       return $result;
    }
    
    function getlast_foo($version_id,$id){

       $this->db->where('category_id',$id);
       $this->db->where('version_id !=',$version_id);
       $this->db->where('count !=',0);
       $query = $this->db->get('send');
       $result = $query->result(); 
       return $result;
    }
    

    
    function main_select_chart($start,$end){

       $sql = "select DISTINCT category_id from send ";
       if($start!=''){
         $sql.= " where createtime between ".$start." and ".$end ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    function select_count($category_id){
       $this->db->select_sum('count');
       $this->db->where('category_id',$category_id);
       $query = $this->db->get('send');       
       $result = $query->result(); 

       return $result;    
    }
    
    function getcom($id){
    	$this->db->select('company_id');
        $this->db->where(array('category_id'=>$id));
    	$this->db->distinct();
        $query = $this->db->get('send');
        $result = $query->result(); 
        return $result;
    }
    
    
    
    function getlast($version_id,$id){
       $this->db->where(array('category_id'=>$id,'version_id'=>$version_id));
       $this->db->select_sum('count');
       $query = $this->db->get('send');
       $result = $query->result(); 
       return $result;
    }
    
    
    function getcount($id,$version_id=''){
        $where['category_id']=$id;
    	if($version_id!=""){
    	 $where['version_id']=$version_id;
    	}
        $this->db->where($where);
    	$this->db->select_sum('count');
        $query = $this->db->get('send');
        $result = $query->result(); 
        return $result;
    }
    
    function main_select_charts($where){
    	
    	$this->db->where($where);
    	$query = $this->db->get('send');
    	$result = $query->result();
    	return $result;
    }
    
    
    function main_select($array){

       $sql = "select DISTINCT send.mod_id,models.name as mod_name, company.name as com_name,send.category_id,send.company_id,category.name as cate_name,send.count as c from send right join category on send.category_id = category.category_id right join models on models.mod_id = send.mod_id right join company on company.company_id = send.company_id where ".$array .' group by send.category_id';
       $query = $this->db->query($sql);
       //echo $this->db->last_query();exit;
       $result = $query->result();
       return $result;
    }
    
    function main_select_mod($array){
    
    	$sql = "select DISTINCT send.mod_id,models.name from send right join models on models.mod_id = send.mod_id where ".$array ;
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function main_select_join($array){
    
    	$sql = "select category.name,category.category_id from send join category on send.category_id=category.category_id where ".$array ;
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    
    
    function main_select_com($array){

       $sql = "select DISTINCT send.company_id,company.name from send left join company on company.company_id = send.company_id where ".$array ;
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function update($id,$array){
       $result = $this->db->update('send',$array,array('send_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('send',array('send_id'=>$id));
       return $result;
    }
}